package com.fgs.common.toolbar;

import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;

import com.fgs.common.R;

/**
 * @author fgs
 * @date 2020/6/30
 * @description
 **/
public class ToolbarStatus {
    ToolbarOptions toolbarOptions;
    IToolbar.OnToolbarClickListener onToolbarClickListener;
    Toolbar toolbar;
    TextView titleTextView;
    TextView subtitleTextView;
    ImageView backIconImageView;
    RelativeLayout backRelativeLayout;

    public ToolbarStatus() {
    }

    public void setToolbarOptions(ToolbarOptions toolbarOptions) {
        this.toolbarOptions = toolbarOptions;
    }

    public void setOnToolbarClickListener(IToolbar.OnToolbarClickListener onToolbarClickListener) {
        this.onToolbarClickListener = onToolbarClickListener;
    }

    public ToolbarOptions getToolbarOptions() {
        return toolbarOptions;
    }

    public void initToolbar() {
        toolbar = toolbarOptions.getToolbar();
        titleTextView = toolbarOptions.getTitleTextView();
        subtitleTextView = toolbarOptions.getSubtitleTextView();
        backIconImageView = toolbarOptions.getBackIconImageView();
        backRelativeLayout = toolbarOptions.getBackRelativeLayout();

        setTitle(toolbarOptions.getTitle());
        setSubtitle(toolbarOptions.getSubtitle());
        isShowToolbar(toolbarOptions.isToolbarShow());
        isShowBackIcon(toolbarOptions.isBackIconShow());
        setBackIconImageView(toolbarOptions.getIconId());

        subtitleTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onToolbarClickListener != null)
                    onToolbarClickListener.onSubtitleClick();
            }
        });

        backRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onToolbarClickListener != null && backIconImageView.getVisibility() == View.VISIBLE)
                    onToolbarClickListener.onBackIconClick();
            }
        });
    }

    public void setTitle(String title) {
        if (titleTextView != null) {
            titleTextView.setText(title);
        }
    }

    public void setSubtitle(String subtitle) {
        if (subtitleTextView != null) {
            subtitleTextView.setText(subtitle);
        }
    }

    public void setSubtitleColor(int color) {
        if (subtitleTextView != null) {
            subtitleTextView.setTextColor(color);
        }
    }

    public void isShowToolbar(boolean isShow) {
        if (toolbar != null) {
            if (isShow) {
                toolbar.setVisibility(View.VISIBLE);
            } else {
                toolbar.setVisibility(View.GONE);
            }
        }
    }

    public void isShowBackIcon(boolean isShow) {
        if (backIconImageView != null) {
            if (isShow) {
                backIconImageView.setVisibility(View.VISIBLE);
            } else {
                backIconImageView.setVisibility(View.GONE);
            }
        }
    }

    public void setBackIconImageView(int iconId) {
        if (backIconImageView != null && iconId > 0) {
            backIconImageView.setBackgroundResource(iconId);
        }
    }

    //    /**
//     * 显示菜单图标
//     */
//    protected void showMenuIcon() {
//        menuIconImageView.setVisibility(View.VISIBLE);
//    }
//
//    /**
//     * 显示菜单图标
//     */
//    protected void showMenuIcon(int iconId) {
//        menuIconImageView.setBackgroundResource(iconId);
//        menuIconImageView.setVisibility(View.VISIBLE);
//    }
//
//    /**
//     * 显示菜单图标
//     */
//    protected void showMenuIcon(int iconId, int width, int height) {
//        Toolbar.LayoutParams layoutParams = (Toolbar.LayoutParams) menuIconImageView.getLayoutParams();
//        layoutParams.width = (int) AppUtils.convertDpToPixel(width);
//        layoutParams.height = (int) AppUtils.convertDpToPixel(height);
//        menuIconImageView.setLayoutParams(layoutParams);
//        menuIconImageView.setBackgroundResource(iconId);
//        menuIconImageView.setVisibility(View.VISIBLE);
//    }
//
//    /**
//     * 隐藏菜单图标
//     */
//    protected void hideMenuIcon() {
//        menuIconImageView.setVisibility(View.GONE);
//    }
//
//    /**
//     * 获取页面副标题
//     */
//    protected String getSubtitle() {
//        return subtitleTextView.getText().toString();
//    }
//
//    protected void setToolbarBackgroundColor(int color) {
//        toolbar.setBackgroundColor(getResources().getColor(color));
//    }
//
//    protected void backIconShow() {
//        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//    }
}
